package com.gp.ssmplus.base.core.mybatis.model;


import com.alibaba.fastjson.util.TypeUtils;
import com.gp.ssmplus.base.core.mybatis.common.utils.FlymeUtils;
import com.gp.ssmplus.base.core.mybatis.common.utils.JsonUtils;
import lombok.Data;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * 分页参数
 *
 * @author zyf
 * @date 2018/07/10
 */
@Data
public class PageModel {
    private int page = 1;

    private int limit = 10;

    private String sort;

    private String order;

    private String handlerName;

    private String params;

    private Boolean isSort = false;

    public PageModel(HttpServletRequest request) {
        Map maps = request.getParameterMap();
        Map<String, Object> params = new HashMap<>();
        for (Iterator iter = maps.entrySet().iterator(); iter.hasNext(); ) {
            Map.Entry element = (Map.Entry) iter.next();
            String strKey = element.getKey().toString();
            String[] strObj = (String[]) element.getValue();
            if (strKey.equals("pageNo")) {
                this.page = FlymeUtils.getInteger(TypeUtils.castToInt(strObj[0]), 1);
            }
            if (strKey.equals("pageSize")) {
                this.limit = FlymeUtils.getInteger(TypeUtils.castToInt(strObj[0]), 10);
            }
            if (strKey.equals("handlerName")) {
                this.handlerName = strObj[0];
            }
            params.put(strKey, strObj[0]);
        }
        String json = JsonUtils.beanToJson(params);
        this.params = json;
    }


}
